home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / A-B / add images.cpt / pix_io.c < prev    next >
Text File  |  1989-06-22  |  10KB  |  418 lines

  1. /*
  2.     pix_io.c
  3.  
  4.     file input and output of PICTs and cut and paste with CLIPBOARD
  5.     and most important - conversion from std picts to the proper colors
  6.     for PROTON, FLOURINE, and the mixture of the two
  7.  
  8. */
  9.  
  10. #include "pix_io.h"
  11. #include "about_alert.h"
  12. #include <IM1_5Protos.h>
  13.  
  14. extern short add_mode;
  15.  
  16. #define      TRUE   1
  17. #define      FALSE  0
  18. #define      NIL    0
  19.  
  20. /* conversion to the proper pixmap could be tricky */
  21.  
  22. short short_max( short a, short b );
  23. short short_min( short a, short b );
  24.  
  25. short short_max( a, b )
  26.     short a;
  27.     short b;
  28. {
  29.     if ( a > b )
  30.         return( a );
  31.     else
  32.         return( b );
  33. }
  34.  
  35. short short_min( a, b )
  36.     short a;
  37.     short b;
  38. {
  39.     if ( a < b )
  40.         return( a );
  41.     else
  42.         return( b );
  43. }
  44.  
  45. short GetPic( theReply, source_pic )
  46.     SFReply theReply;
  47.     PicHandle *source_pic;
  48. /*
  49.     reads PICT file and stores in the
  50.     the pic handle. called by read_proton
  51.     and read_flourine.
  52. */
  53. {
  54.     SFTypeList theList;
  55.     Point p;
  56.     short err, FileRef;
  57.     long BufSize;
  58.     PicHandle tempPic;
  59.  
  60.     /* first, open PICT file */
  61.     FileRef = 0;
  62.  
  63.     SetVol(NIL, theReply.vRefNum);
  64.     err = FSOpen( (Str255 *)&theReply.fName, theReply.vRefNum, &FileRef);
  65.  
  66.     if ( err == noErr )
  67.     {
  68.         err = GetEOF(FileRef, &BufSize);
  69.  
  70.         if (err == noErr)
  71.         {
  72.             /* size of the file includes a 512-byte header */
  73.             BufSize -= 512;
  74.             err = SetFPos(FileRef, fsFromStart, 512);
  75.  
  76.             if ( err == noErr )
  77.             {
  78.                 tempPic = (PicHandle)NewHandle(BufSize);
  79.                 err = MemErr;
  80.  
  81.                 if ( err == noErr )
  82.                 {
  83.                     /* read the file (except the header) into the pic-handle */
  84.                     HLock( (Handle)tempPic);
  85.                     err = FSRead( FileRef, &BufSize, *tempPic );
  86.                     HUnlock( (Handle)tempPic);
  87.                     if ( err == noErr )
  88.                     {
  89.                         *source_pic = tempPic;
  90.                         FileRef = FSClose(FileRef);
  91.                         return( TRUE );
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     /* if we reach here, then we didn't successfully read in the image */
  98.     if (FileRef != 0)
  99.     {
  100.         FileRef = FSClose(FileRef);
  101.     }
  102.  
  103.     D_error_alert( (long) err );
  104.     if (tempPic != NIL)
  105.     {
  106.         DisposHandle( (Handle) tempPic );
  107.     }
  108.     return( FALSE );
  109. }
  110.  
  111. void PutPic( theReply, result_pic, thePalette )
  112. /* write the Pixel image into a PICT file that uses thePalette */
  113.     SFReply theReply;
  114.     PicHandle result_pic;
  115.     PaletteHandle thePalette;
  116. {
  117.  
  118. }
  119.  
  120. typedef unsigned char *BytePtr;
  121.  
  122. CGrafPtr pic_to_bmap( picH, frame, themaxdevice )
  123. /* create off-screen pixmap and draw picture into it */
  124.     PicHandle picH;
  125.     Rect *frame;
  126.     GDHandle themaxdevice;
  127. {
  128.     CGrafPtr off_port;
  129.     GDHandle olddevice;
  130.     GrafPtr saveport;
  131.     long size, offrowbytes;
  132.     Ptr image_space;
  133.     RgnHandle visregion;
  134.     short err, thedepth, i;
  135.     CTabHandle ourCMHandle;
  136.  
  137.  
  138.     /* check that the arguments makes sense */
  139.     if ( (*frame).top >= (*frame).bottom 
  140.         || (*frame).left >= (*frame).right 
  141.         || picH == NIL )
  142.     {
  143.         SysBeep(10);
  144.         return( NIL );
  145.     }
  146.  
  147. /* create first off-screen pixmap, using grayscale, to get images into 0-255 intensity scale */
  148.  
  149.     off_port = (CGrafPtr) NewPtr( sizeof(CGrafPort) );
  150.  
  151.     err = MemErr;
  152.     if ( err != noErr )
  153.     {
  154.         if ( off_port != NIL )
  155.         {
  156.             DisposPtr (off_port);
  157.         }
  158.         D_error_alert( (long) err );
  159.         return(NIL);
  160.     }
  161.     else
  162.     {
  163.         olddevice = GetGDevice();
  164.         GetPort(&saveport);
  165.  
  166.         SetGDevice( themaxdevice );
  167.  
  168.         OpenCPort(off_port);
  169.  
  170.         thedepth = (**off_port->portPixMap).pixelSize;
  171.         offrowbytes = (((thedepth * (frame->right - frame->left)) + 15) >> 4) << 1;
  172.         size = (frame->bottom - frame->top) * (long) offrowbytes;
  173.  
  174.         off_port->portPixMap = NewPixMap(); /* assume error-free ? */
  175.         image_space = NewPtr( size );
  176.     
  177.         err = MemErr;
  178.         if ( err != noErr )
  179.         {
  180.             SetGDevice(olddevice);
  181.             SetPort(saveport);
  182.             if ( off_port != NIL )
  183.             {
  184.                 DisposPtr (off_port);
  185.             }
  186.             if ( image_space != NIL )
  187.             {
  188.                 DisposPtr (image_space);
  189.             }
  190.             D_error_alert( (long) err );
  191.             return(NIL);
  192.         }
  193.         else
  194.         {
  195.             (**(off_port->portPixMap)).rowBytes = offrowbytes + 0x8000;
  196.             (**(off_port->portPixMap)).baseAddr = image_space;
  197.             (**(off_port->portPixMap)).bounds = *frame; 
  198.             
  199.             ourCMHandle = (**(**themaxdevice).gdPMap).pmTable;
  200.             err = HandToHand( & ourCMHandle );
  201.  
  202.             if ( err != noErr )
  203.             {
  204.                 SetGDevice(olddevice);
  205.                 SetPort(saveport);
  206.                 if ( off_port != NIL )
  207.                 {
  208.                     DisposPtr (off_port);
  209.                 }
  210.                 if ( image_space != NIL )
  211.                 {
  212.                     DisposPtr (image_space);
  213.                 }
  214.                 D_error_alert( (long) err );
  215.                 return(NIL);
  216.             }
  217.             else
  218.             {
  219.                 for ( i = 0; i <= (**ourCMHandle).ctSize; ++i )
  220.                 {
  221.                     (**ourCMHandle).ctTable[i].value = i;
  222.                 }
  223.                 (**ourCMHandle).ctFlags &= 0x7fff;
  224.                 (**ourCMHandle).ctSeed = GetCTSeed();
  225.                 /* the above code is needed for converting gdevice cluts to pixmap cluts */
  226.  
  227.                 (**(*off_port).portPixMap).pmTable = ourCMHandle;
  228.  
  229.                 SetPort((GrafPtr) off_port);
  230.  
  231.                 RectRgn(off_port->visRgn, frame);
  232.                 ClipRect( frame );
  233.             
  234.                 DrawPicture(picH, frame);
  235.                 SetGDevice(olddevice);
  236.  
  237.                 SetPort(saveport);
  238.  
  239.                 return( off_port );
  240.             }
  241.         }
  242.     }
  243. }
  244.  
  245. void add_bmaps(  proton_bmap,  proton_rect, pr_off_top, pr_off_left,
  246.                 flourine_bmap,  flourine_rect, fl_off_top, fl_off_left,
  247.                  result_bmap,  result_rect, themaxdevice )
  248. /* create off-screen pixmap and the other two pixmaps into it */
  249.     CGrafPtr proton_bmap;
  250.     Rect *proton_rect;
  251.     CGrafPtr flourine_bmap;
  252.     Rect *flourine_rect;
  253.     CGrafPtr *result_bmap;
  254.     Rect *result_rect;
  255.     short pr_off_top, pr_off_left;
  256.     short fl_off_top, fl_off_left;
  257.     GDHandle themaxdevice;
  258. {
  259.     CGrafPtr off_port;
  260.     GDHandle olddevice;
  261.     GrafPtr saveport;
  262.     long size, offrowbytes;
  263.     short r_top, r_left;
  264.     Ptr image_space;
  265.     Rect pr_off, fl_off;
  266.     RgnHandle visregion;
  267.  
  268.     short err, thedepth, i;
  269.     CTabHandle ourCMHandle;
  270.  
  271.     if ( pr_off_top < proton_rect->bottom      /* test offsets for reasonableness */
  272.         && pr_off_left < proton_rect->right
  273.         && fl_off_top < flourine_rect->bottom
  274.         && fl_off_left < flourine_rect->right 
  275.         && proton_rect->bottom > proton_rect->top /* test frames for sanity */
  276.         && proton_rect->right > proton_rect->left
  277.         && flourine_rect->bottom > flourine_rect->top
  278.         && flourine_rect->right > flourine_rect->left 
  279.         && proton_rect->top == 0                    /* these should always be 0 */
  280.         && proton_rect->left == 0
  281.         && flourine_rect->top == 0
  282.         && flourine_rect->left == 0 )
  283.     {
  284.         result_rect->top = 0;
  285.         result_rect->left = 0;
  286.         pr_off = *proton_rect;
  287.         fl_off = *flourine_rect;
  288.         pr_off.top += pr_off_top;
  289.         pr_off.bottom += pr_off_top;
  290.         pr_off.left += pr_off_left;
  291.         pr_off.right += pr_off_left;
  292.         fl_off.top += fl_off_top;
  293.         fl_off.bottom += fl_off_top;
  294.         fl_off.right += fl_off_left;
  295.         fl_off.left += fl_off_left;
  296.         r_top = short_max( pr_off_top, fl_off_top );
  297.         r_left = short_max( pr_off_left, fl_off_left );
  298.         result_rect->right = r_left + short_max( proton_rect->right - pr_off_left,
  299.             flourine_rect->right - fl_off_left );
  300.         result_rect->bottom = r_top + short_max( proton_rect->bottom - pr_off_top,
  301.             flourine_rect->bottom - fl_off_top );
  302.  
  303.         off_port = (CGrafPtr) NewPtr( sizeof(CGrafPort) );
  304.         
  305.         err = MemErr;
  306.         if ( err != noErr )
  307.         {
  308.             if ( off_port != NIL )
  309.             {
  310.                 DisposPtr (off_port);
  311.             }
  312.             D_error_alert( (long) err );
  313.             *result_bmap = NIL;
  314.             return;
  315.         }
  316.         else
  317.         {
  318.             olddevice = GetGDevice();
  319.             GetPort(&saveport);
  320.  
  321.             SetGDevice( themaxdevice );
  322.  
  323.             OpenCPort(off_port);
  324.             thedepth = (**off_port->portPixMap).pixelSize;
  325.             offrowbytes = (((thedepth * (result_rect->right - result_rect->left)) + 15) >> 4) << 1;
  326.             size = (result_rect->bottom - result_rect->top) * (long) offrowbytes;
  327.  
  328.             off_port->portPixMap = NewPixMap(); /* assume error-free ? */
  329.             image_space = NewPtr( size );
  330.             err = MemErr;
  331.             if ( err != noErr )
  332.             {
  333.                 SetGDevice(olddevice);
  334.                 SetPort(saveport);
  335.                 if ( off_port != NIL )
  336.                 {
  337.                     DisposPtr (off_port);
  338.                 }
  339.                 if ( image_space != NIL )
  340.                 {
  341.                     DisposPtr (image_space);
  342.                 }
  343.                 D_error_alert( (long) err );
  344.                 *result_bmap = NIL;
  345.                 return;
  346.             }
  347.             else
  348.             {
  349.                 (**(off_port->portPixMap)).rowBytes = offrowbytes + 0x8000;
  350.                 (**(off_port->portPixMap)).baseAddr = image_space;
  351.                 (**(off_port->portPixMap)).bounds = *result_rect; 
  352.                 ourCMHandle = (**(**themaxdevice).gdPMap).pmTable;
  353.                 err = HandToHand( & ourCMHandle );
  354.  
  355.                 if ( err != noErr )
  356.                 {
  357.                     SetGDevice(olddevice);
  358.                     SetPort(saveport);
  359.                     if ( off_port != NIL )
  360.                     {
  361.                         DisposPtr (off_port);
  362.                     }
  363.                     if ( image_space != NIL )
  364.                     {
  365.                         DisposPtr (image_space);
  366.                     }
  367.                     D_error_alert( (long) err );
  368.                     *result_bmap = NIL;            
  369.                     return;
  370.                 }
  371.                 else
  372.                 {
  373.                     for ( i = 0; i <= (**ourCMHandle).ctSize; ++i )
  374.                     {
  375.                         (**ourCMHandle).ctTable[i].value = i;
  376.                     }
  377.                     (**ourCMHandle).ctFlags &= 0x7fff;
  378.                     (**ourCMHandle).ctSeed = GetCTSeed();
  379.                     /* the above code is needed for converting gdevice cluts to pixmap cluts */
  380.                     (**(*off_port).portPixMap).pmTable = ourCMHandle;
  381.  
  382.                     SetPort((GrafPtr) off_port);
  383.  
  384.                     RectRgn(off_port->visRgn, result_rect);
  385.                     ClipRect( result_rect );
  386.  
  387.                     /* copy over the bitmaps */
  388.                     EraseRect( result_rect);
  389.  
  390.                     HLock((Handle) (off_port->portPixMap));
  391.  
  392.                     HLock((Handle) (proton_bmap->portPixMap));
  393.                     CopyBits( (BitMap *) *(proton_bmap->portPixMap), 
  394.                               (BitMap *) *(off_port->portPixMap), 
  395.                             proton_rect, &pr_off, (short) srcCopy, 
  396.                             (RgnHandle) NIL);
  397.                     HUnlock((Handle) (proton_bmap->portPixMap));
  398.  
  399.                     HLock((Handle) (flourine_bmap->portPixMap));
  400.                     CopyBits( (BitMap *) *(flourine_bmap->portPixMap), 
  401.                               (BitMap *) *(off_port->portPixMap), 
  402.                             flourine_rect, &fl_off, (short) add_mode, 
  403.                             (RgnHandle) NIL);
  404.                     HUnlock((Handle) (flourine_bmap->portPixMap));
  405.  
  406.                     HUnlock((Handle) (off_port->portPixMap));
  407.  
  408.                     SetGDevice(olddevice);
  409.  
  410.                     SetPort(saveport);
  411.                     *result_bmap = off_port;
  412.                     return;
  413.                 }
  414.             }
  415.         }
  416.     }/* end if args reasonable */
  417. }
  418.